const Base = require('./base.js');


module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.rule = this.model("system/rule");
	}
	async indexAction() {
		let pid = this.get("pid");
		let rules = await this.rule.getChilds(pid);

		if(this.isPost) {
			return this.success(rules);
		}
		this.assign("rules", rules);
		return this.display();
	}

	async childAction() {

		let pid = this.post("pid");
		let childs = await this.rule.getChilds(pid);
		this.assign("rules", childs);
		return this.display();

	}

	async addAction() {
		if(this.isGet) {
			let pid = this.get("pid");

			if(pid) {
				let rule = await this.rule.findOne(pid);
				this.assign("parentRule", rule);

			}
			return this.display();

		}

		let posts = this.post();

		let rule_id = await this.rule.add({
			name: posts.name,
			code: posts.code,
			target: posts.target,
			url: posts.url,
			pid: posts.parentId,
			status: posts.disabled ? 0 : 1,
			icon: posts.icon,
			type: posts.type,
			auth: posts.auth ? 1 : 0
		});

		if(rule_id == 0) {
			return this.fail("添加失败");
		}

		await this.rule.where({
			id: posts.parentId
		}).update({
			hasChildren: 1
		});

		return this.success("添加成功");

	}

	async treeAction() {

		let pid = this.get("pid");
		let rules = await this.rule.getChilds(pid);
		var str = [];
		if(!think.isEmpty(rules)) {

			rules.forEach((o) => {
				let obj = {};
				obj.id = o.id;
				obj.text = o.name;
				obj.children = o.hasChildren == 1 ? true : false;
				str.push(obj);
			});
		}

		return this.json(str);
	}

	async detailAction() {

		let id = this.get("id");
		let rule = await this.rule.findOne(id);
		this.assign("rule", rule);

		return this.display();

	}

	async editAction() {

		if(this.isGet) {
			let id = this.get("id");

			if(id) {
				let rule = await this.rule.findOne(id);

				if(rule.pid) {
					let parentRule = await this.rule.findOne(rule.pid);

					this.assign("parentRule", parentRule);

				}

				this.assign("rule", rule);
				return this.display();
			}

		}

		let posts = this.post();

		let rows = await this.rule.setOne(posts.id, {
			name: posts.name,
			code: posts.code,
			target: posts.target,
			url: posts.url,
			pid: posts.parentId,
			icon: posts.icon
		});

		if(rows == 0) {
			return this.fail("修改失败");
		}

		await this.rule.setOne(posts.parentId, {
			hasChildren: 1
		});

		return this.success("修改成功");

	}

	async deleteAction() {

		let id = this.post("id");
		let rows = await this.rule.where({
			id: id
		}).delete();
		if(rows == 0) {
			return this.fail("删除失败");
		}
		return this.success("删除成功");

	}

	async statusAction() {

		let id = this.post("id");
		let status = this.post("status");
		let row = await this.rule.setOne(id, {
			status: status
		});

		if(row == 0) {
			return this.fail("修改失败");
		}

		return this.success();

	}

	async sortAction() {

		if(this.isGet) {

			let firstMenus = [],
				secondMenus = {};

			let rules = await this.rule.order('sort asc').select();
			rules.forEach(function(obj) {
				if(obj.code.split(":").length == 1) {
					if(JSON.stringify(firstMenus).indexOf(JSON.stringify(obj)) < 0 && obj.type == 1) {
						firstMenus.push(obj);

					}

				} else {
					let s = secondMenus[obj.pid] || [];
					if(JSON.stringify(s).indexOf(JSON.stringify(obj)) < 0 && obj.type == 1) {
						s.push(obj);
					}
					secondMenus[obj.pid] = s;
				}

			});

			this.assign("firstMenus", firstMenus);
			this.assign("secondMenus", secondMenus);
			return this.display();

		}

		let i = 0,
			j = 0,
			k = 0;
		let ids = JSON.parse(this.post("ids"));
		let self = this;

		ids.forEach(async(f) => {
			i++;
			let fid = f.id;
			await self.rule.setOne(fid, {
				sort: i
			});
			if(f.children) {
				f.children.forEach(async(s) => {
					j++;
					let sid = s.id;
					await self.rule.setOne(sid, {
						sort: j
					});

					if(s.children) {
						s.children.forEach(async(t) => {
							k++;
							let tid = t.id;
							await self.rule.setOne(tid, {
								sort: k
							});;

						});

					}
				});

			}
		});
		return this.success("修改成功");

	}
};